import requests
from lxml import etree
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route("/")
def index():
    return render_template('index.html')


@app.route("/search_phone")
def search_phone():
    phone = request.args.get('phone')
    data = get_mobile(phone)
    return data


# @app.route("/search_weather")
# def search_weather():
#     weather = request.args.get('weather')
#     weather_info = get_weather('weather')
#     return weather_info


def get_mobile(phone):
    url = f'https://ip138.com/mobile.asp?mobile={phone}&action=mobile'

    headers = {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
    }

    resp = requests.get(url=url, headers=headers)
    resp.encoding = 'utf-8'

    tree = etree.HTML(resp.text)

    info = tree.xpath('//tr/td[2]/span/text()')
    return info


# def get_weather(city):
#     url = f'https://www.ks121.com/search/?location={city}'
#     headers = {
#         'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
#     }
#
#     resp = requests.get(url=url, headers=headers)
#
#     tree = etree.HTML(resp.text)
#     align_list = tree.xpath('//tr/td/a/text()')
#     print(align_list)
#
#     # 将['星期三', '2025-07-16', '\n', '\n', '\n', '\n', '\n', '18℃ ～ 31℃', '北风 3-4级转<3级']替换为['星期三', '2025-07-16', '18℃ ～ 31℃', '北风 3-4级转<3级']
#     weather = [item.rstrip() for item in align_list if item.rstrip()]
#
#     # return weather
#     print(weather)

app.run(debug=True)
